import Vue from 'vue';
import VueRouter from 'vue-router';

const Layout = () => import('@/layout');
const Login = () => import('@/views/login');
const Mall = () => import('@/views/mall');
const Product = () => import('@/views/product');
// const Subscriber = () => import('@/views/userinfo/subscriber.vue');
const Material = () => import('@/views/material');
const MaterialInfo = () => import('@/views/material/material-info');
const ProductInfo = () => import('@/views/product/product-info');
const Collect = () => import('@/views/collect');
const RemitRecord = () => import('@/views/remit-record');
const FinishedProduct = () => import('@/views/finished-product');
const ShopManagement = () => import('@/views/shop-management');
const ProductStencil = () => import('@/views/product/product-stencil');
const SkuManagement = () => import('@/views/sku-management');
const PricingStencil = () => import('@/views/pricing-stencil');
const PricingManagement = () =>
  import('@/views/pricing-stencil/pricing-management.vue');

Vue.use(VueRouter);

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录',
    },
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    redirect: '/mall',
    children: [
      {
        path: '/mall',
        name: 'mall',
        component: Mall,
        meta: {
          title: '首页',
          keepAlive: true,
        },
      },
      {
        path: '/product',
        name: 'product',
        component: Product,
        meta: {
          title: '产品市场',
          keepAlive: true,
        },
      },
      {
        path: '/product-info',
        name: 'productInfo',
        component: ProductInfo,
        meta: {
          title: '产品详情',
        },
      },
      {
        path: '/material',
        name: 'material',
        component: Material,
        meta: {
          title: '素材市场',
          keepAlive: true,
        },
      },
      {
        path: '/material-info',
        name: 'materialInfo',
        component: MaterialInfo,
        meta: {
          title: '素材详情',
        },
      },
      {
        path: '/collect',
        name: 'collect',
        component: Collect,
        meta: {
          title: '我的收藏',
          keepAlive: true,
        },
      },
      {
        path: '/finished-product',
        name: 'finishedProduct',
        component: FinishedProduct,
        meta: {
          title: '成品管理',
        },
      },
      {
        path: '/remit-record',
        name: 'remitRecord',
        component: RemitRecord,
        meta: {
          title: '汇出记录',
        },
      },
      {
        path: '/shop-management',
        name: 'shopManagement',
        component: ShopManagement,
        meta: {
          title: '店铺管理',
        },
      },
      {
        path: '/product-stencil',
        name: 'productStencil',
        component: ProductStencil,
        meta: {
          title: '产品模板设置',
        },
      },
      {
        path: '/sku-management',
        name: 'skuManagement',
        component: SkuManagement,
        meta: {
          title: 'SKU管理',
        },
      },
      {
        path: '/pricing-stencil',
        name: 'pricingStencil',
        component: PricingStencil,
        meta: {
          title: '定价模板',
        },
      },
      {
        path: '/pricing-management',
        name: 'pricingManagement',
        component: PricingManagement,
        meta: {
          title: '定价模板管理',
        },
      },
      // {
      //   path: '/subscriber',
      //   name: 'Subscriber',
      //   component: Product,
      //   meta: {
      //     title: '修改用户资料',
      //   },
      // },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  // 设置浏览器标题
  if (to.meta.title) {
    document.title = to.meta.title;
  } else {
    document.title = '亚马逊电商';
  }
  next();
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => err);
};

const RouterReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(to) {
  return RouterReplace.call(this, to).catch((err) => err);
};

export default router;
